Vgg16提取特征并可视化 |
您所在的位置:网站首页 › vgg 16 › Vgg16提取特征并可视化 |
目录:
导入基础库构建多特征层输出模型中间层显示
导入基础库
导入基础包 import numpy as np import matplotlib.pyplot as plt import tensorflow as tf导入vgg16网络层 VGG16_model = tf.keras.applications.VGG16(include_top=True) VGG16_model.summary()读取本地病害图像(这里我是用苹果叶片病害图像) def prepocess(x): x = tf.io.read_file(x) x = tf.image.decode_jpeg(x, channels=3) print(x.shape) x = tf.image.resize(x, [224,224]) x = tf.cast(x, dtype=tf.float32)/255. return x img_path='E:/AA-shiyanData/Vgg_Data/train/Apple___Apple_scab/001.JPG' img=prepocess(img_path) plt.figure() plt.imshow(img)对模型中16层特征层建立输出layers和多输出model模型 from tensorflow.keras import models layer_outputs = [layer.output for layer in VGG16_model.layers[:16]] #前16层输出 activation_model = models.Model(inputs=VGG16_model.input, outputs=layer_outputs) #构建能够输出前16层的模型图片输入网络,输出各中间层的特征 input_image=tf.expand_dims(img, 0) # 扩维 activations = activation_model.predict(input_image) #12组特征层输出 activations[0].shape #0对应summary表中的输入层举例展示第3卷积层的第1和第2特征层 plt.matshow(activations[3][0,:,:,0], cmap='viridis') #第3卷积层的第1特征层输出 plt.matshow(activations[3][0,:,:,1], cmap='viridis') #第3卷积层的第0特征层输出
这结果确实符合随着层数的加深,激活变得越来越抽象,以及激活的稀疏度(sparsity)随着层数的加深而增大的预期结果。 参考别的博客,写下来只为日后自己复制用。 大家可以一起交流科研学习 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |